package com.untrance.shop.core.builder.vo;

import com.untrance.shop.common.utils.JpaUtils;
import com.untrance.shop.common.vo.AbstractVO;
import com.untrance.shop.model.MainEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author eugene zadyra
 */
public abstract class AbstractVOBuilder<From extends MainEntity, To extends AbstractVO> {

	public To createVO(From entity) {
		if (entity == null) {
			return null;
		}
		return transform(entity);
	}

	public List<To> createVOList(List<From> entities) {
		if (entities == null) {
			return null;
		}
		List<To> list = new ArrayList<To>();
		for (From entity : entities) {
			list.add(updateKey(entity, transform(entity)));
		}
		return list;
	}

	private To updateKey(From entity, To vo) {
		if (entity != null && vo != null) {
			vo.setKey(JpaUtils.getString(entity.getId()));
		}
		return vo;
	}

	protected abstract To transform(From object);

}
